//---------------------------------------------------------------------
// File: FileDeleteExStep.cs
// 
// Summary: 
//
// Copyright (c) Hammersmith & Fulham Bridge Partnership. All rights reserved.
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
// KIND, WHETHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
// PURPOSE.
//---------------------------------------------------------------------

using System;
using System.IO;
using System.Xml;
using Microsoft.Services.BizTalkApplicationFramework.BizUnit;

namespace BizUnit.Extensions
{
	/// <summary>
	/// The FileDeleteStep deletes a FILE specified at a given location.
	/// The EXTENSION now includes the use of context in this step
	/// </summary>
	/// 
	/// <remarks>
	/// The following shows an example of the Xml representation of this test step.
	/// 
	/// <code escaped="true">
	///	<TestStep assemblyPath="BizUnit.Extensions.dll" typeName="BizUnit.Extensions.FileDeleteExStep">
	///		<FileToDeletePath takeFromCtx="c:\file1.xml"></FileToDeletePath>
	///		<FileToDeletePath takeFromCtx="c:\file2.xml"></FileToDeletePath>
	///	</TestStep>
	///	</code>
	///	
	///	<list type="table">
	///		<listheader>
	///			<term>Tag</term>
	///			<description>Description</description>
	///		</listheader>
	///		<item>
	///			<term>FileToDeletePath</term>
	///			<description>The location of FILE to be deleted<para>(one or more)</para></description>
	///		</item>
	///	</list>
	///	</remarks>

	public class FileDeleteExStep : ITestStep
	{
		/// <summary>
		/// ITestStep.Execute() implementation
		/// </summary>
		/// <param name='testConfig'>The Xml fragment containing the configuration for this test step</param>
		/// <param name='context'>The context for the test, this holds state that is passed beteen tests</param>
		public void Execute(XmlNode testConfig, Context context)
		{
			// BUGFIX - Will. Edited this step to enable it to read file locations from context 
			string fileName = string.Empty;
			try
			{
				int fileCount = testConfig.SelectNodes("FileToDeletePath").Count;

				for(int i = 1; i <= fileCount; i++)
				{
					// set the fileName by using the context reader. The context reader expects a nodeset and an xpath expression
					// it proved difficult to break the nodeset up and just pass it the single element it needs so pass the lot and index the XPath
					fileName = context.ReadConfigAsString(testConfig, String.Format("FileToDeletePath[{0}]",i.ToString()));
					File.Delete ( fileName );
					context.LogInfo( String.Format("FileDeleteStep has deleted file: {0}", fileName ));
				}
			}
			catch(Exception ex)
			{
				string myMsg = String.Format("There was a problem while trying to delete the file at {0}. This step requires an xml element or context called 'FileToDeletePath' - please check this value.", fileName);
				Exception myEx = new Exception(myMsg, ex);
				throw myEx;
			}
		}
	}
}
